<template>
  <div class="page-container fixed-bottom">
    <Steps :step="step" :status="status" :step-list="stepList" @showReason="rejectHandle" />
    <el-form ref="form" :model="form" label-width="170px" disabled>
      <el-form-item label="套票类型：">
        <el-radio-group v-model="form.ticketPackageType">
          <el-radio :label="1">同店铺内套票</el-radio>
          <el-radio :label="2">本企业下跨店铺套票</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="参加店铺：" prop="shop">
        <div v-if="form.shopList && form.shopList.length">
          <el-tag v-for="item in form.shopList" :key="item.shopId" effect="plain">
            {{ item.shopName }}
          </el-tag>
        </div>
      </el-form-item>
      <el-form-item label="有效时间：" prop="timeList">
        <el-date-picker
          v-model="form.timeList"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="套票产品：" prop="product">
        <p>已选择产品数：{{ form.ticketProductBaseVOS.length }}个</p>
        <div class="ticket-item">
          <el-table border :data="form.ticketProductBaseVOS">
            <el-table-column label="产品名称" prop="productName" />
            <el-table-column label="产品规格" prop="" />
            <el-table-column label="规格单位" prop="" />
            <el-table-column label="价格（元）" prop="productPrice" />
            <el-table-column label="产品数量">
              <template slot-scope="scope">
                <el-input v-model="scope.row.productNum" placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column label="套票价格（元）" prop="">
              <template slot-scope="scope">
                <el-input v-model="scope.row.packagePrice" placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column label="店铺名称" prop="shopName" />
            <el-table-column label="项目名称" prop="projectName" />
          </el-table>
        </div>
      </el-form-item>
      <el-form-item label="套票总价：" prop="retailPrice">
        <el-input v-model="form.retailPrice" class="min-input" clearable />
      </el-form-item>
      <el-form-item label="套票库存：" prop="dailyInventory">
        <el-input v-model="form.dailyInventory" class="min-input" clearable />
      </el-form-item>
      <el-form-item label="产品图片：" prop="productImg">
        <UploadFile :file-list="form.productImg" @change="uploadHandle($event, 'productImg','productImg')" />
      </el-form-item>
      <el-form-item label="套票名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="套票简介：">
        <el-input v-model="form.introduction" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="购买须知：" prop="playInstructions">
        <el-input v-model="form.playInstructions" placeholder="请输入" type="textarea" rows="5" clearable />
      </el-form-item>
      <el-form-item label="详情" prop="illustrate">
        <RichText :value.sync="form.illustrate" disabled />
      </el-form-item>
      <el-form-item label="标签：">
        <el-checkbox-group v-model="form.labels">
          <el-checkbox label="0">特惠</el-checkbox>
          <el-checkbox label="1">特价</el-checkbox>
          <el-checkbox label="2">景区自营</el-checkbox>
          <el-checkbox label="3">谁买使用</el-checkbox>
          <el-checkbox label="4">每日抄底</el-checkbox>
          <el-checkbox label="5">惠民补贴</el-checkbox>
          <el-checkbox label="6">十亿补贴</el-checkbox>
          <el-checkbox label="7">无需换票</el-checkbox>
          <el-checkbox label="8">提前预定</el-checkbox>
          <el-checkbox label="9">限时抢购</el-checkbox>
          <el-checkbox label="10">全网低价</el-checkbox>
          <el-checkbox label="11">今日必买</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="套票栏目：" prop="ticketColumn">
        <el-radio-group v-model="form.ticketColumn">
          <el-radio :label="0">吃美食</el-radio>
          <el-radio :label="1">游景区</el-radio>
          <el-radio :label="2">景区门票+住宿</el-radio>
          <el-radio :label="3">镇街通票</el-radio>
          <el-radio :label="4">其他</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否支持使用优惠券：">
        <el-radio-group v-model="form.supportCoupon">
          <el-radio :label="0">不支持</el-radio>
          <el-radio :label="1">支持</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div v-if="type ==='approve'" class="bottom-handle-box">
      <el-button type="primary" @click="approveHandle">通过</el-button>
      <el-button type="danger" @click="rejectHandle">驳回</el-button>
    </div>
    <Reject
      v-if="showReject"
      :type="type"
      :reject-form="rejectForm"
      @confirm="confirmHandle"
      @close="closeHandle"
    />
  </div>
</template>

<script>
import UploadFile from '@/components/UploadFile/index.vue'
import RichText from '@/components/RichText/index.vue'
import Steps from './Steps.vue'
import Reject from '@/components/Approve/Reject.vue'
import { getTicketDetail, approveTicket, rejectTicket } from '@/api/approve'
import { getShopListByShopId } from '@/api/common'

export default {
  name: 'TicketDetail',
  components: {
    Steps,
    UploadFile,
    RichText,
    Reject
  },
  data() {
    return {
      id: '',
      type: '',
      step: 1,
      stepList: [],
      status: -1,
      rejectForm: {},
      showReject: false,
      form: {
        ticketPackageType: '',
        shopList: [],
        timeList: [],
        ticketProductBaseVOS: [],
        retailPrice: '',
        dailyInventory: '',
        productImg: '',
        name: '',
        introduction: '',
        playInstructions: '',
        illustrate: '',
        labels: [],
        ticketColumn: '',
        supportCoupon: ''
      }
    }
  },

  created() {
    const { id, type } = this.$route.query
    this.id = id
    this.type = type
    if (this.id) {
      this.getDetail()
    }
  },

  methods: {
    getDetail() {
      const data = {
        id: this.id
      }
      getTicketDetail(data).then(res => {
        const {
          createTime,
          accountName,
          approveTime,
          approveUserStr,
          approveStatusStr,
          approveStatus,
          rejectReason,
          rejectFileUrl,
          ticketPackageCreateReq
        } = res || {}
        this.status = approveStatus
        if (this.status === 0) {
          this.step = 1
        } else {
          this.step = 3
        }
        this.rejectForm = {
          rejectReason,
          rejectFileUrl
        }
        this.stepList = [
          {
            title: '提交申请',
            time: createTime,
            user: accountName
          },
          {
            title: '审核员审核',
            time: approveTime,
            user: approveUserStr
          },
          {
            title: '完成',
            time: approveTime,
            status: approveStatusStr
          }
        ]
        const {
          ticketPackageType,
          shopIds,
          startDate,
          endDate,
          ticketProductBaseVOS,
          retailPrice,
          dailyInventory,
          productImg,
          name,
          introduction,
          playInstructions,
          illustrate,
          labels,
          ticketColumn,
          supportCoupon
        } = ticketPackageCreateReq || {}
        this.form = {
          ticketPackageType,
          // shopList: shopIds.split(','),
          timeList: [startDate, endDate],
          ticketProductBaseVOS,
          retailPrice,
          dailyInventory,
          productImg,
          name,
          introduction,
          playInstructions,
          illustrate,
          labels: labels.split(','),
          ticketColumn,
          supportCoupon
        }
        this.getShopList(shopIds)
      }).catch(() => {})
    },

    // 获取门店列表
    getShopList(ids) {
      const data = {
        ids: ids.split(',')
      }
      getShopListByShopId(data).then(res => {
        this.$set(this.form, 'shopList', res || [])
      }).catch(() => {})
    },

    // 返回
    goBack() {
      this.$router.back()
    },

    // 通过
    approveHandle() {
      this.$confirm('确定要审核通过改申请吗？', '提示', {
        type: 'warning'
      }).then(() => {
        const data = {
          id: this.id
        }
        approveTicket(data).then(res => {
          this.$message.success('审核成功')
          this.goBack()
        }).catch(() => {})
      })
    },

    // 驳回
    rejectHandle() {
      this.showReject = true
    },

    // 关闭
    closeHandle() {
      this.showReject = false
    },

    // 确定驳回
    confirmHandle(form) {
      const data = {
        id: this.id,
        ...form
      }
      rejectTicket(data).then(() => {
        this.$message.success('驳回成功')
        this.showReject = false
        this.goBack()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.ticket-item{
  margin-top: 10px;
}
.min-input{
  width: 150px;
  margin-right: 10px;
}
.el-tag{
  margin-right: 10px;
}
</style>
